package com.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 测试消费者
 * 
 * @author Administrator
 *
 */
public class Consumer {
	public static void main(String[] args) {
		// 测试常规服务
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-consumer.xml");

		// *注意：启动Spring容器,如果调用了getBean方法,容器默认就会随之启动,可以不显式的调用start方法
		context.start();
		
		printAllBeans(context);

		System.out.println("consumer start");
		
		DemoService demoService = context.getBean(DemoService.class);
		
		// 如果使用类名去获取bean，会获取不到，因为类名经过dubbo的代理已经发生了改变
		// DemoService service = (DemoService) context.getBean("demoService");
		// 但是我们在配置消费者的引用服务dubbo:reference时，指明了的id可以用作实现IOC注入，此方法也是可行的
		// DemoService service = (DemoService) context.getBean("permissionService");
		
		List<String> listPermissions = demoService.listPermissions(8L);

		listPermissions.forEach(str -> {
			System.out.println("consumer消费：" + str);
		});
	}

	public static void printAllBeans(ApplicationContext context) {
		String[] beans = context.getBeanDefinitionNames();
		for (String beanName : beans) {
			Class<?> beanType = context.getType(beanName);
			System.out.println("BeanName:" + beanName);
			System.out.println("Bean的类型：" + beanType);
			System.out.println("Bean所在的包：" + beanType.getPackage());
			System.out.println("Bean：" + context.getBean(beanName));
		}
	}

}
